package com.jing;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jing.bean.User;
import com.jing.bean.UserQuery;
import com.jing.dao.UserDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class AppTest01 {
    @Autowired
    private UserDao userDao;

    @Test
    public void test01() {
        //查询用户年龄大于18
        //1.创建条件查询对象
        //QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        //where age > 18
        //gt 大于  lt 小于 ge 大于等于 le 小于等于
        //queryWrapper.gt("age",18);
        //queryWrapper.lambda().gt(User::getAge,18);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.gt(User::getAge,18);
        //2.执行查询
        List<User> userList = userDao.selectList(queryWrapper);
        System.out.println(userList);
    }
    @Test
    public void test02() {
        //年龄大于18小于30的用户
        //1.先创建查询条件
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //2.添加查询条件
        //lambdaQueryWrapper.gt(User::getAge,18);
        //lambdaQueryWrapper.lt(User::getAge,30);
        //在mybatisplus 中多个条件默认and
        lambdaQueryWrapper.gt(User::getAge,18).lt(User::getAge,30);
        List<User> userList = userDao.selectList(lambdaQueryWrapper);
        System.out.println(userList);
    }
    @Test
    public void test03() {
        //查询年龄小于18岁 大于30岁 的用户
        //1.创建查询条件对象
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        //2.添加查询条件
        lambdaQueryWrapper.lt(User::getAge,18);
        lambdaQueryWrapper.or();
        lambdaQueryWrapper.gt(User::getAge,30);
        List<User> usersList = userDao.selectList(lambdaQueryWrapper);
        System.out.println(usersList);
    }
    @Test
    public void test04() {
        //1.创建前端请求模拟条件
        UserQuery userQuery = new UserQuery();
        userQuery.setAge2(30);
        userQuery.setAge(18);
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        if (userQuery.getAge() != null) {
//            lambdaQueryWrapper.gt(User::getAge,userQuery.getAge());
//
//        }
//        if (userQuery.getAge2() != null) {
//            lambdaQueryWrapper.lt(User::getAge,userQuery.getAge2());
//        }
        lambdaQueryWrapper.gt(userQuery.getAge() != null,User::getAge,userQuery.getAge());
        lambdaQueryWrapper.lt(userQuery.getAge2() != null,User::getAge,userQuery.getAge2());
        List<User> userList = userDao.selectList(lambdaQueryWrapper);
        System.out.println(userList);
    }
    @Test
    public void test05() {

        //LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //lambdaQueryWrapper.select(User::getId,User::getName,User::getTel,User::getAge);
        QueryWrapper<User> lambdaQueryWrapper = new QueryWrapper<>();
        lambdaQueryWrapper.select("id","name","age","tel");
        List<User> usersList = userDao.selectList(lambdaQueryWrapper);
        System.out.println(usersList);
    }
    @Test
    public void test06() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("count(*) as count , tel");
        queryWrapper.groupBy("tel"); // 分组
        List<Map<String, Object>> maps = userDao.selectMaps(queryWrapper);
        System.out.println(maps);
    }
    @Test
    public void test07() {
        //登录
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getName,"tom").eq(User::getPassword,"123456");
        //执行查询
        User user = userDao.selectOne(lambdaQueryWrapper);
        System.out.println(user);
    }
    @Test
    public void test08() {
        //范围
        //1.创建查询对象
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.between(User::getAge,18,30);
        List<User> userList = userDao.selectList(lambdaQueryWrapper);
        System.out.println(userList);
    }
    @Test
    public void test09() {
        //模糊查询
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //lambdaQueryWrapper.like(User::getName,"j"); //查询包含j 的内容
        //lambdaQueryWrapper.likeRight(User::getName,"j"); //以 j 开头的内容
        lambdaQueryWrapper.likeLeft(User::getName,"j"); //以 j 结尾的
        List<User> userList = userDao.selectList(lambdaQueryWrapper);
        System.out.println(userList);
    }
    @Test
    public void test10() {
        //排序
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //lambdaQueryWrapper.orderByAsc(User::getAge); //升序
        //a
        //lambdaQueryWrapper.orderByDesc(User::getAge);  //降序
        lambdaQueryWrapper.orderBy(true,false,User::getAge);
        List<User> userList = userDao.selectList(lambdaQueryWrapper);
        System.out.println(userList);

    }
    @Test
    public void test11() {
        //批量查询
        List<Long> list = new ArrayList<>();
        list.add(1L);
        list.add(3L);
        list.add(5L);
        list.add(7L);
        List<User> userList = userDao.selectBatchIds(list);
        //int i = userDao.deleteBatchIds(list); //批量删除
        System.out.println(userList);
    }
    @Test
    public void test12() {

    }

}
